<template>
	<view class="chat">
		<uni-nav-bar :fixed="true" border="false" color="#000" background-color="#F8F8F8" left-icon="phone"
			right-icon="list" @clickLeft="callphone" @clickRight="toOrder">
			<view class="city" style="color: #27ba9b;">订单进度：</view>
			<view class="city" @click="toJindu">{{progressData}}</view>
		</uni-nav-bar>
		<scroll-view :style="{height: `${windowHeight}rpx`}" id="scrollview" scroll-y="true" :scroll-top="scrollTop"
			:scroll-with-animation="true" class="scroll-view">
			<!-- 聊天主体 -->
			<view id="msglistview" class="chat-body">
				<!-- 聊天记录 -->
				<view v-for="(item,index) in msgList" :key="index">
					<!-- 自己发的消息 -->
					<view class="item self" v-if="item.userAccount == account">
						<!-- 文字内容 -->
						<view class="content right">
							{{item.msg}}
						</view>
						<!-- 头像 -->
						<view class="avatar">
							<image :src="apiUrl + item.pic"></image>
						</view>
					</view>
					<!-- 其他人发的消息 -->
					<view class="item Ai" v-if="item.userAccount != account">
						<!-- 头像 -->
						<view class="avatar">
							<image :src="apiUrl + item.pic"></image>
						</view>
						<!-- 文字内容 -->
						<view class="content left">
							{{item.msg}}
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<!-- 底部消息发送栏 -->
		<!-- 用来占位，防止聊天消息被发送框遮挡 -->
		<view class="chat-bottom">
			<view class="send-msg">
				<view class="uni-textarea">
					<textarea v-model="chatMsg" maxlength="300" :show-confirm-bar="false" auto-height></textarea>
				</view>
				<button @click="handleSend" class="send-btn">发送</button>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		apiUrl
	} from '@/config/config.js';
	import AvatarImage from '@/components/avatar-image';
	export default {
		data() {
			return {
				//滚动距离
				scrollTop: 0,
				userId: '',
				orderId: '',
				//发送的消息
				chatMsg: "",
				msgList: null,
				apiUrl: apiUrl,
				account: '',
				progressData: '',

			}
		},
		computed: {
			windowHeight() {
				return this.rpxTopx(uni.getSystemInfoSync().windowHeight)
			}
		},
		onLoad(options) {
			this.orderId = options.id //接受上一个页面传过来的参数
			this.account = uni.getStorageSync('account_key')
		},
		onShow() {
			this.loadList()
			this.progress()
			console.log("onShow")
		},
		methods: {
			callphone() {

			},
			toOrder() {
				uni.navigateTo({
					url: `/pages/order/chat/od?id=${this.orderId}`
				});
			},
			toJindu() {
				uni.navigateTo({
					url: `/pages/order/jindu?id=${this.orderId}`
				});
			},
			// px转换成rpx
			rpxTopx(px) {
				let deviceWidth = wx.getSystemInfoSync().windowWidth
				let rpx = (750 / deviceWidth) * Number(px)
				return Math.floor(rpx)
			},
			// 发送消息
			handleSend() {
				//如果消息不为空
				if (!this.chatMsg || !/^\s+$/.test(this.chatMsg)) {
					// let obj = {
					// 	userAccount: this.account,
					// 	msg: this.chatMsg
					// }
					// this.msgList.push(obj);
					this.sendMsg(this.chatMsg)
					this.chatMsg = '';
				} else {
					this.$modal.showToast('不能发送空白消息')
				}
			},
			sendMsg(msg) {
				uni.request({
					url: this.apiUrl + "/api/group/send",
					method: 'GET',
					data: {
						orderId: this.orderId,
						msg: msg
					},
					header: {
						account: this.account
					},
					success: res => {
						// this.msgList = res.data.data;
						this.loadList()
					},
					fail: res => {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 1000
						});
					}
				});
			},
			loadList() {
				uni.request({
					url: this.apiUrl + "/api/group/get",
					method: 'GET',
					data: {
						orderId: this.orderId,
					},
					success: res => {
						this.msgList = res.data.data;
					},
					fail: res => {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 1000
						});
					}
				});
			},
			progress() {
				uni.request({
					url: this.apiUrl + "/api/group/progress",
					method: 'GET',
					data: {
						orderId: this.orderId,
					},
					success: res => {
						this.progressData = res.data.msg;
					},
				});
			},
		}
	}
</script>
<style lang="scss" scoped>
	$chatContentbgc: #C2DCFF;
	$sendBtnbgc: #4F7DF5;

	view,
	button,
	text,
	input,
	textarea {
		margin: 0;
		padding: 0;
		box-sizing: border-box;
	}

	.city {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		// width: 160rpx;
		margin-left: 4px;
	}

	/* 聊天消息 */
	.chat {
		.scroll-view {
			::-webkit-scrollbar {
				display: none;
				width: 0 !important;
				height: 0 !important;
				-webkit-appearance: none;
				background: transparent;
				color: transparent;
			}

			// background-color: orange;
			background-color: #F6F6F6;

			.chat-body {
				display: flex;
				flex-direction: column;
				padding-top: 23rpx;
				// background-color:skyblue;

				.self {
					justify-content: flex-end;
				}

				.item {
					display: flex;
					padding: 23rpx 30rpx;
					// background-color: greenyellow;

					.right {
						background-color: $chatContentbgc;
					}

					.left {
						background-color: #FFFFFF;
					}

					// 聊天消息的三角形
					.right::after {
						position: absolute;
						display: inline-block;
						content: '';
						width: 0;
						height: 0;
						left: 100%;
						top: 10px;
						border: 12rpx solid transparent;
						border-left: 12rpx solid $chatContentbgc;
					}

					.left::after {
						position: absolute;
						display: inline-block;
						content: '';
						width: 0;
						height: 0;
						top: 10px;
						right: 100%;
						border: 12rpx solid transparent;
						border-right: 12rpx solid #FFFFFF;
					}

					.content {
						position: relative;
						max-width: 486rpx;
						border-radius: 8rpx;
						word-wrap: break-word;
						padding: 24rpx 24rpx;
						margin: 0 24rpx;
						border-radius: 5px;
						font-size: 32rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #333333;
						line-height: 42rpx;
					}

					.avatar {
						display: flex;
						justify-content: center;
						width: 78rpx;
						height: 78rpx;
						background: $sendBtnbgc;
						border-radius: 8rpx;
						overflow: hidden;

						image {
							align-self: center;
							width: 100%;
							height: 100%;
						}

					}
				}
			}
		}

		/* 底部聊天发送栏 */
		.chat-bottom {
			width: 100%;
			height: 177rpx;
			background: #F4F5F7;

			.send-msg {
				display: flex;
				align-items: flex-end;
				padding: 16rpx 30rpx;
				width: 100%;
				min-height: 177rpx;
				position: fixed;
				bottom: 0;
				background: #EDEDED;
			}

			.uni-textarea {
				padding-bottom: 70rpx;

				textarea {
					width: 537rpx;
					min-height: 75rpx;
					max-height: 500rpx;
					background: #FFFFFF;
					border-radius: 8rpx;
					font-size: 32rpx;
					font-family: PingFang SC;
					color: #333333;
					line-height: 43rpx;
					padding: 5rpx 8rpx;
				}
			}

			.send-btn {
				display: flex;
				align-items: center;
				justify-content: center;
				margin-bottom: 70rpx;
				margin-left: 25rpx;
				width: 128rpx;
				height: 75rpx;
				background: $sendBtnbgc;
				border-radius: 8rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 28rpx;
			}
		}

	}
</style>